
FIUMAPS.Search = {Tracker:null,
    ResetResults: function(){
        $('searchresults').innerHTML = "";
        showSearchInstructions();
    },
    ExecuteFind: function(searchText) {
    
        this.InitiateResultsTracker();
        // Find Task
        var findTask = new esri.arcgis.gmaps.FindTask("http://maps.fiu.edu/arcgis/rest/services/FIUCampusMaps/ServicesNBuildings/MapServer/");

        // Find Parameters
        var params = new esri.arcgis.gmaps.FindParameters();
        params.layerIds = [0,1];
        params.searchFields = ["NAME","CODE","KEYWORDS","DESCRIPTION"];
        //params.returnGeometry = false;

        // set find parameters
        params.searchText = searchText;
        
        // execute find task
        findTask.execute(params, FIUMAPS.Search.FindComplete_Callback);
        
    },
    FindComplete_Callback: function(fset){

        // clear map overlays
        ClearOverlays();
        // clear html search results
        $('searchresults').innerHTML = "";

        var results = fset.findResults, attributes, geometry;
        
        var Tracker = [];
        
        //loop through features  
        for (var i = 0; i < results.length; i++) {
            attributes = results[i].feature.attributes;
            geometry = results[i].feature.geometry;
            // get campus code.
            var campus_code = attributes.CAMPUS_CODE;
          
            //if campus not added to tracker, add it by adding DIV header it.
            if(!FIUMAPS.Search.Tracker[campus_code].DIV){
                FIUMAPS.Search.createDIVheader(attributes.CAMPUS_NAME, campus_code);           
            }
            
            // create html list item element and href link.
            var li = document.createElement('li');            
            var link = FIUMAPS.Search.createLinkElement(attributes.NAME);
            
            
            if(attributes.SERVICE_ID){ //it's a service
                //if LocationType UL is not present then create it.
                if(!FIUMAPS.Search.Tracker[campus_code].Services_UL){
                    //Create UL and store it.
                    FIUMAPS.Search.Tracker[campus_code].Services_UL = document.createElement('ul');
                }
                // Services/GMarker //Add ListItem to Service UL 
                //add hover rules to all geometries associated to this feature.
                addPointListeners(geometry,attributes,link);

                // add link to list item container
                li.appendChild(link);
                // append list item to unordered list.
                FIUMAPS.Search.Tracker[campus_code].Services_UL.appendChild(li);                        
                    
            }else{ //it's a building.
                if(!FIUMAPS.Search.Tracker[campus_code].Building_UL){
                    //Create UL and store it.
                    FIUMAPS.Search.Tracker[campus_code].Building_UL = document.createElement('ul');
                }        
                //Add ListItem to Building UL  
                //add hover rules to all geometries associated to this feature.
                addPolygonListeners(geometry,attributes,link);
                // add link to list item container
                li.appendChild(link);
                // append list item to unordered list.
                FIUMAPS.Search.Tracker[campus_code].Building_UL.appendChild(li);                     
                    
            }


            
        } 
        
        if(results.length > 0){ // has results
            //loop through all campuses attaching the LocationType ULs.
            for (var z = 0; z < FIUMAPS.CampusInfo.arrData.length; z++) {
                //only attach if the DIV has something in it.
                if(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV){
                    //attach Building LocationType UL to Div if present
                    if(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].Building_UL){
                        FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV.appendChild(FIUMAPS.Search.createH5Element("Buildings"));
                        FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV.appendChild(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].Building_UL);
                    }
                    //attach Services LocationType UL to Div if present
                    if(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].Services_UL){
                        FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV.appendChild(FIUMAPS.Search.createH5Element("Services"));
                        FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV.appendChild(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].Services_UL);            
                    }
                    
                    //attach div to search results.
                    $('searchresults').appendChild(FIUMAPS.Search.Tracker[FIUMAPS.CampusInfo.arrData[z].Code].DIV);
                }
                
            }        
        }else{ //no results

            // attach "No Results"
            $('searchresults').appendChild(FIUMAPS.Search.createH5Element("No Results Found"));       
       
        }
        
        showSearchResultsDiv();
        
    },     
    InitiateResultsTracker: function() {
        //load campus cache structure from CampusInfo Data.
        //Will loop number of campus times.
        var tmpArr = [];
        for (var z = 0; z < FIUMAPS.CampusInfo.arrData.length; z++) {
            tmpArr.push(FIUMAPS.CampusInfo.arrData[z].Code + ":{DIV:null,Building_UL:null,Services_UL:null}");
        }      
        var JSONString = "FIUMAPS.Search.Tracker = {" + tmpArr.join(',') + "}"; 
        eval(JSONString); // Execute the javascript code contained in JSONString.
        
    },     
    createDIVheader: function(campus_name, campus_code) {
        //Create Div and store it.
        FIUMAPS.Search.Tracker[campus_code].DIV = document.createElement('DIV');
        //Create Campus Header and store it.
        var h4 = document.createElement('h4');
        h4.appendChild(document.createTextNode(campus_name));   
        FIUMAPS.Search.Tracker[campus_code].DIV.appendChild(h4);  
        
    },     
    createLinkElement: function(loc_name) {
        // create html link element 
        var link = document.createElement('a');
        link.style.cursor = 'pointer';
        link.style.marginBottom = '5px';
        link.title = loc_name;
        link.appendChild(document.createTextNode(loc_name)); 
        
        return link;
    },     
    createH5Element: function(text) {
        // create html link element 
        var h5 = document.createElement('h5');
        h5.appendChild(document.createTextNode(text));
        
        return h5;
    }    
            
}